package com.keywaysoft.workflow.controller;

import com.keywaysoft.workflow.constants.WorkflowConstants;
import com.keywaysoft.workflow.dto.*;
import com.keywaysoft.workflow.service.IProcessEngine;
import com.keywaysoft.workflow.utils.Page;
import com.keywaysoft.workflow.utils.R;
import com.keywaysoft.workflow.vo.ActRuTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author zhaojx<br>
 * @version 1.0<br>
 * @createDate 2021/11/16 17:34 <br>
 * @Description <p> 任务相关接口 </p>
 */

@RestController
@Api(tags = "任务相关接口")
@Slf4j
@RequestMapping("task")
public class TaskController {

    @Autowired
    IProcessEngine processEngine;


    @PostMapping(value = "/query/all")
    @ApiOperation(value = "查询所有任务", notes = "查询所有任务")
    public R<Page<ActRuTask>> taskQuery(@RequestBody FindTaskByAssigneeDTO dto) {
        return processEngine.taskQuery(dto);
    }


    @PostMapping(path = "todo/list")
    @ApiOperation(value = "查询当前用户的待办任务", notes = "查询当前用户的待办任务")
    public R<Page<ActRuTask>> findTaskByAssignee(@RequestBody FindTaskByAssigneeDTO dto) {
        return processEngine.findTaskByAssignee(dto);
    }

    @PostMapping(path = "get/variables")
    @ApiOperation(value = "根据任务key查询当前任务自定义信息", notes = "根据任务key查询当前任务自定义信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务ID", dataType = "String", paramType = "query")
    })
    public R getCurrentUserTaskVariables(@RequestParam String taskId) {
        return processEngine.getCurrentUserTaskVariables(taskId);
    }

    @PostMapping(path = "get/comments")
    @ApiOperation(value = "根据实例ID查询当前实例所有审批意见", notes = "根据实例ID查询当前实例所有审批意见")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processInstanceId", value = "实例ID", dataType = "String", paramType = "query")
    })
    public R getProcessInstanceComments(@RequestParam String processInstanceId) {
        return processEngine.getProcessInstanceComments(processInstanceId);
    }

    @PostMapping(path = "complete")
    @ApiOperation(value = "完成任务", notes = "完成任务，任务进入下一个节点")
    public R completeTask(@RequestBody CompleteTaskDTO dto) {
        return processEngine.completeTask(dto);
    }


    @PostMapping(path = "turnTask")
    @ApiOperation(value = "任务转办", notes = "任务转办，把任务交给别人处理")
    public R turnTask(@RequestBody TaskBaseDTO dto) {
        return processEngine.turnTask(dto);
    }

    /**
     * 委派任务
     *
     * @return
     */
    @PostMapping(value = "delegate")
    @ApiOperation(value = "委派任务", notes = "委派任务")
    public R delegateTask(@RequestBody TaskBaseDTO dto) {
        return processEngine.delegateTask(dto);
    }

    /**
     * 委派人完成任务
     *
     * @return
     */
    @PostMapping(value = "resolve")
    @ApiOperation(value = "委派人完成任务", notes = "委派人完成任务")
    public R resolveTask(@RequestBody TaskDTO dto) {
        return processEngine.resolveTask(dto);
    }

    /**
     * 驳回任务
     *
     * @return r
     */
    @PostMapping(value = "back/task")
    @ApiOperation(value = "驳回任务", notes = "驳回任务")
    public R putBackTask(@RequestBody BackTaskDTO dto) {
        if(StringUtils.isBlank(dto.getComment())){
            dto.setComment(WorkflowConstants.BACK_COMMENT);
        }
        processEngine.backToPreNode(dto);
        return R.success();
    }


    @GetMapping(value = "/get/nodeList")
    @ApiOperation(value = "获取可以驳回的节点", notes = "获取可以驳回的节点")
    public R getNodeList(@RequestParam String taskId) {
        return processEngine.getNodeList(taskId);
    }


    /**
     * 驳回指定环节
     *
     * @return r
     */
    @PostMapping(value = "/back/any")
    @ApiOperation(value = "驳回指定环节", notes = "驳回指定环节")
    public R putBack2Task(@RequestBody TargetBackTaskDTO dto) {
        processEngine.back2Node(dto);
        return R.success();
    }

    /**
     * 驳回至首环节
     *
     * @return r
     */
    @PostMapping(value = "/back/first")
    @ApiOperation(value = "驳回至首环节", notes = "驳回至首环节")
    public R putBackFirstTask(@RequestBody BackTaskDTO dto) {
        processEngine.backToFirstNode(dto);
        return R.success();
    }

    /**
     * 撤回任务
     *
     * @return r
     */
    @PostMapping(value = "withdraw/task")
    @ApiOperation(value = "撤回任务", notes = "撤回任务")
    public R withdrawTask(@RequestBody TaskBaseDTO dto) {
        processEngine.withdrawApproval(dto);
        return R.success();
    }

    @PostMapping(value = "end/task")
    @ApiOperation(value = "结束任务", notes = "结束任务")
    public R endTask(@RequestBody BackTaskDTO dto) {
        processEngine.endTask(dto);
        return R.success();
    }

        /**
     * 签收任务
     *
     * @return
     */
    @PostMapping(value = "claim")
    @ApiOperation(value = "签收任务", notes = "签收任务")
    public R claim(@RequestBody TaskBaseDTO dto) {
        return processEngine.claim(dto);
    }

    /**
     * 取回签收任务
     *
     * @return
     */
    @PostMapping(value = "unclaim")
    @ApiOperation(value = "取回签收任务", notes = "取回签收任务")
    public R unclaim(@RequestBody TaskBaseDTO dto) {
        return processEngine.unclaim(dto);
    }

}